//
//  SEGScratchCardView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/11/23.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGScratchCardView.h"

@implementation SEGScratchCardView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self customUI];
    }
    return self;
}

#pragma mark - 初始化界面

- (void)customUI
{
    self.userInteractionEnabled = YES;

    //背景视图
    self.bgView = [[UIImageView alloc] init];
    self.bgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"bg_dialog_guaka")];
    //    self.bgView.layer.masksToBounds = YES;
    //    self.bgView.layer.cornerRadius = 5;
    self.bgView.userInteractionEnabled = YES;
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).with.offset(0);
        make.width.mas_equalTo(270);
        make.height.mas_equalTo(344);
    }];

    //关闭按钮
    self.closeButton = [[UIButton alloc] init];
    self.closeButton.backgroundColor = [UIColor clearColor];
    [self.closeButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_dialog_close_w")] forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.bgView.mas_top);
        make.right.equalTo(self.bgView.mas_right);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];

    //图片
    self.cardBgView = [[UIImageView alloc] init];
    self.cardBgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_dialog_guaka01")];
    self.cardBgView.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:self.cardBgView];
    [self.cardBgView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.bgView.mas_left).with.offset(15);
        make.bottom.equalTo(self.bgView.mas_bottom).with.offset(-58);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(125);
    }];

    //奖项背景
    self.prizeContentView = [[UIView alloc] initWithFrame:CGRectMake(35, 178, 199, 87)];
    self.prizeContentView.backgroundColor = [UIColor colorWithRed:249 / 255.0 green:248 / 255.0 blue:226 / 255.0 alpha:1];
    [self.bgView addSubview:self.prizeContentView];

    //谢谢参与
    self.titleLabel = [[UILabel alloc] initWithFrame:self.prizeContentView.bounds];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    self.titleLabel.textColor = kColor_S1;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.backgroundColor = [UIColor clearColor];
    [self.prizeContentView addSubview:self.titleLabel];

    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 159, 35)];
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    self.nameLabel.textColor = kColor_S1;
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    self.nameLabel.backgroundColor = [UIColor clearColor];
    self.nameLabel.numberOfLines = 0;
    [self.prizeContentView addSubview:self.nameLabel];
    self.nameLabel.hidden = YES;

    self.cardFrontView = [[SEGImageMaskView alloc] initWithFrame:CGRectMake(35, 178, 199, 87) image:[UIImage imageWithContentsOfFile:SEGPicName(@"img_dialog_guaka02")]];
    //    self.cardFrontView.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:self.cardFrontView];

    __weak typeof (self) weakSelf = self;
    self.cardFrontView.completion = ^() {
        [weakSelf showResult];
    };

    //跳转按钮
    self.goButton = [[UIButton alloc] init];
    [self.goButton setTitleColor:kColor_S2 forState:UIControlStateNormal];
    [self.goButton addTarget:self action:@selector(goButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.goButton.titleLabel.font = [UIFont systemFontOfSize:12];
    self.goButton.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:self.goButton];
    [self.goButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.centerY.equalTo(self.bgView.mas_bottom).with.offset(-29);
        //        make.width.mas_equalTo(240);
        //        make.height.mas_equalTo(44);
    }];
    self.goButton.enabled = NO; //默认不能点击 中奖后才可点击
}

- (void)closeButtonClick:(UIButton*)sender
{
    [self removeFromSuperview];
}

- (void)goButtonClick:(UIButton*)sender
{
//    [self.delegate clickViewSoon];
    self.callback([self.dict objectForKey:@"data"]);
    [self removeFromSuperview];
}

- (void)updateViewWithDict:(NSDictionary*)dict callBack:(SEGAwardScratchCradViewCallBack)callback
{
    self.callback = callback;
    self.dict = dict;
    if ([ValueToString([self.dict valueForKey:@"code"]) isEqualToString:@"0"]) { //已中奖
        NSDictionary* data = [self.dict objectForKey:@"data"];
        if (data && [data isKindOfClass:[NSDictionary class]]) {
            self.titleLabel.text = @"恭喜中奖";
            self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
            self.titleLabel.frame = CGRectMake(20, 12, 159, 20);
            self.nameLabel.hidden = NO;
            self.nameLabel.text = [data objectForKey:@"prizeName"];
        }
    }
    else { //未中奖
        self.titleLabel.text = @"谢谢参与";
    }
    [self.goButton setTitle:[NSString stringWithFormat:@"%@保留活动解释权",kAppName] forState:UIControlStateNormal];
}

- (void)showResult
{
    if ([ValueToString([self.dict valueForKey:@"code"]) isEqualToString:@"0"]) { //已中奖
        NSDictionary* data = [self.dict objectForKey:@"data"];
        if (data && [data isKindOfClass:[NSDictionary class]]) {
            [self.goButton setTitle:@"立即查看" forState:UIControlStateNormal];
            self.goButton.titleLabel.font = [UIFont systemFontOfSize:18];
            self.goButton.enabled = YES;
        }
    }
    else { //未中奖
        [self.goButton setTitle:[NSString stringWithFormat:@"%@保留活动解释权",kAppName] forState:UIControlStateNormal];
    }
}
@end
